package com.woniuxy.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.woniuxy.entity.Client;
import com.woniuxy.entity.Department;
import com.woniuxy.mapper.DepartmentMapper;
import com.woniuxy.service.DepartmentService;
import com.woniuxy.utils.KeyUtil;

@Service
public class DepartmentServiceImpl implements DepartmentService{
	@Autowired
	private DepartmentMapper departmentMapper;
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	@Override
	public List<Department> findDepartment() {
		String key=KeyUtil.generate(this, "findDepartment");
		if (redisTemplate.hasKey(key)) {
			List<Object> objects=redisTemplate.opsForList().range(key, 0, redisTemplate.opsForList().size(key));
			List<Department> departments=new ArrayList<Department>();
			ArrayList<Object> data=(ArrayList<Object>) objects.get(0);
			for (Object object : data) {
				departments.add((Department)object);
			}
			return departments;
		}else{
			List<Department> departments=departmentMapper.findDepartment();
			redisTemplate.opsForList().leftPushAll(key, departments);
			redisTemplate.expire(key, 1, TimeUnit.DAYS);
			return departments;
		}
	}
}
